package com.weishaoying.practice.service.impl;

import com.weishaoying.practice.entity.UserDto;
import com.weishaoying.practice.entity.SysPermission;
import com.weishaoying.practice.entity.SysUser;
import com.weishaoying.practice.service.SysService;
import com.weishaoying.practice.util.MD5;
import com.weishaoying.practice.util.PasswordUtil;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysServiceImpl implements SysService {

//    @Autowired
//	private SysUserMapper sysUserMapper;

//	@Autowired
//	private SysPermissionMapperCustom sysPermissionMapperCustom;

    @Override
    public UserDto authenticat(String userCode, String password) {
        System.out.println("-----authenticate--------userCode = " + userCode);
        /**
         认证过程：
         根据用户身份（账号）查询数据库，如果查询不到用户不存在
         对输入的密码 和数据库密码 进行比对，如果一致，认证通过
         */
        //根据用户账号查询数据库
        SysUser sysUser = this.findSysUserByUserCode(userCode);
        if (sysUser == null) {
            //抛出异常
            throw new RuntimeException("用户账号不存在");
        }
        //数据库密码 (md5密码 )
        String password_db = sysUser.getPassword();
        //对输入的密码 和数据库密码 进行比对，如果一致，认证通过
        //对页面输入的密码 进行md5加密
        String password_input_md5 = new MD5().getMD5ofStr(password);
        if (!password_input_md5.equalsIgnoreCase(password_db)) {
            //抛出异常
            throw new RuntimeException("用户名或密码 错误");
        }
        //得到用户id
        String userid = sysUser.getId();
        //根据用户id查询菜单
        List<SysPermission> menus = this.findMenuListByUserId(userid);

        //根据用户id查询权限url
        List<SysPermission> permissions = this.findPermissionListByUserId(userid);

        //认证通过，返回用户身份信息
        UserDto activeUser = new UserDto();
        activeUser.setUserid(sysUser.getId());
        activeUser.setUsercode(userCode);
        activeUser.setUsername(sysUser.getUsername());//用户名称

        //放入权限范围的菜单和url
        activeUser.setMenus(menus);
        activeUser.setPermissions(permissions);

        return activeUser;
    }

    //根据用户账号查询用户信息
    public SysUser findSysUserByUserCode(String userCode) {
        if (!userCode.toLowerCase().equals("weishaoying")) {
            return null;
        }
        SysUser user = new SysUser();
        user.setId("K0000001");
        user.setUsercode(userCode);
        user.setUsername("WeiShaoying");
        //TODO:**********************密码存的应该是加盐MD5之后的**********************
        String credentials = "Abc123";
        String salt = "AK88";
        String password = PasswordUtil.generateCredent(credentials, salt);
        user.setPassword(password);
        user.setSalt(salt);
        //TODO:********************** 密码 **********************
        user.setLocked("0");
        return user;
    }

    @Override
    public List<SysPermission> findMenuListByUserId(String userid) {
        List<SysPermission> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SysPermission p = new SysPermission();
            p.setName("菜单" + i);
            p.setUrl("URL" + i);
            p.setType("Type" + i);
            list.add(p);
        }
        return list;
        //return sysPermissionMapperCustom.findMenuListByUserId(userid);
    }

    @Override
    public List<SysPermission> findPermissionListByUserId(String userid) {
        List<SysPermission> list = new ArrayList<>();
        for (int i = 10; i < 20; i++) {
            SysPermission p = new SysPermission();
            p.setName("菜单B" + i);
            p.setUrl("URL-" + i);
            p.setType("Type-" + i);
            p.setPercode("user:create" + i);
            list.add(p);
        }
        return list;
        //return sysPermissionMapperCustom.findPermissionListByUserId(userid);
    }

    public static void main(String[] args) {
        String credentials = "Abc123";
        String salt = "AK88";
        String password = PasswordUtil.generateCredent(credentials, salt);
        System.out.println("password = " + password);
    }


}